package com.gs.commentvl.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gs.commentvl.model.Post;
import com.gs.commentvl.users.service.PostService;
import com.gs.commentvl.users.service.PostServiceImpl;


public class test {
	public static void main (String[] agr) {
		//test.jsonToObject();
		//test.objectToJson();

		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

		PostService postService = applicationContext.getBean(PostServiceImpl.class);
		Post post = new Post();
		post.setPostId(5L);
		post.setContents("commentvl_Test");
		post.setPostedTime(127L);
		postService.addNewContent(post);

//		Set<Post> posts = postService.getAllPost();
//		for (Post post : posts) {
//			System.out.println(post.getContents());
//		}


		System.out.println("finish....");

	}

	public static void jsonToObject(){
			List<Employee> employee = new ArrayList<Employee>();
	      ObjectMapper mapper = new ObjectMapper();
	      try
	      {
	         employee =  mapper.readValue(new File("c://employee.json"), mapper.getTypeFactory().constructCollectionType(
	                    List.class, Employee.class));
	         Employee s = employee.get(1);
	         System.out.println(s.getFirstName());
	      } catch (JsonGenerationException e)
	      {
	         e.printStackTrace();
	      } catch (JsonMappingException e)
	      {
	         e.printStackTrace();
	      } catch (IOException e)
	      {
	         e.printStackTrace();
	      }
	}

	public static void objectToJson () {
		@SuppressWarnings("deprecation")
		  List<Employee> lstEmp = new ArrayList<Employee>();
	      Employee employee = new Employee(1, "Lokesh", "Gupta", new Date(1981,8,18));
	      lstEmp.add(employee);

	      Employee employee_1 = new Employee(1, "son", "huynh", new Date(1983,05,10));
	      lstEmp.add(employee_1);
	      ObjectMapper mapper = new ObjectMapper();
	      try
	      {
	    	  //mapper.writeValue(new File("c://employee.json"), employee);
	    	  //mapper.writeValue(new File("c://employee.json"), lstEmp);
	    	  String value = mapper.writeValueAsString(lstEmp);
	    	  System.out.println(value);
	      } catch (JsonGenerationException e)
	      {
	         e.printStackTrace();
	      } catch (JsonMappingException e)
	      {
	         e.printStackTrace();
	      } catch (IOException e)
	      {
	         e.printStackTrace();
	      }
	}
}
